package com.seven.monior.TaskInfo;

import java.util.ArrayList;
import java.util.List;

import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.TrafficStats;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;

import com.seven.monior.R;

public class TaskItemAdapter extends BaseAdapter {

	private List<TaskInfo> items;
	private LayoutInflater mInflater;
	private boolean bShowRandT = true;
	private boolean bShowCPU = true;

	@Override
	public void notifyDataSetChanged() {
		// TODO Auto-generated method stub
		// AddData((ArrayList<TaskInfo>) items);

		super.notifyDataSetChanged();
	}

	public TaskItemAdapter(Context ctx) {
		mInflater = LayoutInflater.from(ctx);
		items = new ArrayList<TaskInfo>();
	}

	public void AddData(ArrayList<TaskInfo> mList) {
		items = mList;
	}

	public void setShowRandT(boolean show) {
		bShowRandT = show;
	}

	public void setShowCPU(boolean show) {
		bShowCPU = show;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		TaskInfo item = items.get(position);
		// TaskInfo item = items.get(listWithoutInternalApp.get(position));
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.tasks_list_item, null);
			holder.taskIcon = (ImageView) convertView
					.findViewById(R.id.tasks_item_icon);
			holder.taskName = (TextView) convertView
					.findViewById(R.id.tasks_item_name);
			holder.task_Rx = (TextView) convertView
					.findViewById(R.id.tv_task_Rx);
			holder.task_Tx = (TextView) convertView
					.findViewById(R.id.tv_task_Tx);
			holder.task_Cpu = (TextView) convertView
					.findViewById(R.id.tv_task_cpu);
			holder.task_Mem = (TextView) convertView
					.findViewById(R.id.tv_task_mem);
			holder.tableRow_Rx = (TableRow) convertView
					.findViewById(R.id.tableRow_Rx);
			holder.tableRow_Tx = (TableRow) convertView
					.findViewById(R.id.tableRow_Tx);
			holder.tableRow_CPU = (TableRow) convertView
					.findViewById(R.id.tableRow_CPU);
			holder.tableRow_Mem = (TableRow) convertView
					.findViewById(R.id.tableRow_Mem);

			holder.taskInfos = (TextView) convertView
					.findViewById(R.id.tasks_item_infos);

			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Drawable dIcon = item.getIcon();
		if (dIcon != null) {
			holder.taskIcon.setImageDrawable(dIcon);
		}

		if (item.getApplicationLabelList().size() > 1) {
			holder.taskName.setText((String) item.getApplicationLabel() + " ("
					+ item.getApplicationLabelList().size() + ")");
		} else {
			holder.taskName.setText((String) item.getApplicationLabel());
		}
		holder.task_Rx.setText(item.getMobile_Rx().toString());
		holder.task_Tx.setText(item.getMobile_Tx().toString());
		holder.task_Cpu.setText(String.format("%.1f", item.getTotalCpuTime()));
		holder.task_Mem.setText(String.format("%.1f", item.getMemory()));
		if (bShowRandT) {
			holder.tableRow_Rx.setVisibility(View.VISIBLE);
			holder.tableRow_Tx.setVisibility(View.VISIBLE);
		} else {
			holder.tableRow_Rx.setVisibility(View.GONE);
			holder.tableRow_Tx.setVisibility(View.GONE);
		}
		if (bShowCPU) {
			holder.tableRow_CPU.setVisibility(View.VISIBLE);
			holder.tableRow_Mem.setVisibility(View.VISIBLE);
		} else {
			holder.tableRow_CPU.setVisibility(View.GONE);
			holder.tableRow_Mem.setVisibility(View.GONE);
		}

		holder.taskInfos.setText((String) item.getTaskInfo());

		if (item.getIsRunning())
			convertView.setBackgroundColor(Color.BLACK);
		else {
			convertView.setBackgroundColor(Color.GRAY);
		}

		return convertView;
	}

	private String getTaskInfos(RunningAppProcessInfo item) {
		String strLog = "";
		strLog += "Importance: " + item.importance + "\n";
		strLog += "Pid: " + item.pid + "\n";
		strLog += "ProcessName: " + item.processName + "\n";
		strLog += "Uid: " + item.uid + "\n";
		strLog += "PkgList: ";
		for (String pkgString : item.pkgList) {
			strLog += pkgString + " | ";
		}
		strLog += "\n";
		Long lRXBytes = TrafficStats.getUidRxBytes(item.uid);
		Long lTXBytes = TrafficStats.getUidTxBytes(item.uid);
		strLog += "RX Bytes: " + lRXBytes + "\n";
		strLog += "TX Bytes: " + lTXBytes + "\n";

		return strLog;

	}

	private Drawable getIcon(RunningAppProcessInfo rInfo, PackageManager pm) {
		List<PackageInfo> packageInfos = pm.getInstalledPackages(0);
		for (int i = 0; i < packageInfos.size(); i++) {
			PackageInfo pInfo = packageInfos.get(i);
			if (pInfo.applicationInfo.uid == rInfo.uid) {
				return pInfo.applicationInfo.loadIcon(pm);
			}
		}
		return null;
	}

}

class ViewHolder {
	public ImageView taskIcon;
	public TextView taskName;
	public TextView task_Rx;
	public TextView task_Tx;
	public TextView task_Cpu;
	public TextView task_Mem;

	public TableRow tableRow_Rx;
	public TableRow tableRow_Tx;
	public TableRow tableRow_CPU;
	public TableRow tableRow_Mem;

	public TextView taskInfos;
}
